﻿using OL.FrameCore.Application.Home.Dto;
using OL.FrameCore.Domain.Entity;
using OL.FrameCore.Infrastructure.UnitOfWork;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace OL.FrameCore.Application.Home
{
    public class HomeService: IHomeService
    {
        IUnitOfWork _unitOfwork;
        public HomeService(IUnitOfWork unitOfwork)
        {
            _unitOfwork = unitOfwork;
        }

        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <returns></returns>
        public GetCurrentDataResponse GetCurrentData()
        {
            var repositoryArticle = _unitOfwork.GetRepository<AppArticle>();
            var repositoryUser = _unitOfwork.GetRepository<SysUser>();
            var data = new GetCurrentDataResponse
            {
                NewNum = repositoryArticle.TableNoTracking.Where(p => p.IsDelete == false).Count(),//新闻数
                NoticeNum = repositoryArticle.TableNoTracking.Where(p => p.IsDelete == false).Count(),//公告数
                UserNum = repositoryUser.TableNoTracking.Where(p => p.IsDelete == false).Count()//用户数
            };
            return data;
        }

        /// <summary>
        /// 获取登录统计
        /// </summary>
        /// <returns></returns>
        public IList<GetLoginStatisticsModel> GetLoginStatistics()
        {
            //构建每天登录数据sql，用UNION并成列表
            string sql = "";
            DateTime begin = DateTime.Now.Date.AddDays(-10);
            while (begin <= DateTime.Now.Date)
            {
                if (begin != DateTime.Now.Date.AddDays(-10))
                    sql += " UNION\n";
                sql += $" select '{begin.ToString("MM-dd")}' [Date],count(1) LoginNum from Sys_UserLoginLog where LoginTime>='{begin.ToString("yyyy-MM-dd")}' and LoginTime<'{begin.AddDays(1).ToString("yyyy-MM-dd")}'";
                begin = begin.AddDays(1);
            }
            var list = _unitOfwork.SqlQuery<GetLoginStatisticsModel>(sql);
            return list;
        }

        /// <summary>
        /// 获取用户分布
        /// </summary>
        /// <returns></returns>
        public GetUserDistributionResponse GetUserDistribution()
        {
            var repositoryUser = _unitOfwork.GetRepository<SysUser>();
            var tableUser = repositoryUser.TableNoTracking;
            var data = new GetUserDistributionResponse
            {
                ManNum = tableUser.Where(p => p.IsDelete == false && p.Sex == 0).Count(),//男 数量
                WomanNum = tableUser.Where(p => p.IsDelete == false && p.Sex == 1).Count()//女 数量
            };
            return data;
        }

        /// <summary>
        /// 获取我的登录数据
        /// </summary>
        /// <returns></returns>
        public GetMyLoginDataResponse GetMyLoginData(int userId)
        {
            var repositoryUserLoginLog = _unitOfwork.GetRepository<SysUserLoginLog>();
            //登录次数
            var loginTime = repositoryUserLoginLog.TableNoTracking.Where(p => p.UserId == userId).Count();
            //最后3次登录时间
            var listLast = repositoryUserLoginLog.TableNoTracking.Where(p => p.UserId == userId).OrderByDescending(p => p.LoginTime).Select(p => p.LoginTime.ToString("yyyy-MM-dd HH:mm:ss")).Take(3).ToList();
            
            var data = new GetMyLoginDataResponse
            {
                UserName = "",
                LoginTime = loginTime,
                LoginList = listLast
            };
            return data;
        }
    }
}
